package cc.chenwenxi.sdk.demo;
import com.pi4j.wiringpi.Spi;

public class WiringPiSPIExample {

    // SPI operations
    public static byte WRITE_CMD = 0x40;
    public static byte READ_CMD  = 0x41;

    @SuppressWarnings("unused")
    public static void main(String args[]) throws InterruptedException {
    	//
        //此SPI示例使用WiringPi本机库进行通信
        // SPI硬件接口连接到MCP23S17 I / O扩展器。
        //
        //请注意，启用SPI驱动程序需要以下命令
        //你的Raspberry Pi：
        // > sudo modprobe spi_bcm2708
        // > sudo chown`id -u` .id -g` /dev/spidev0.*
        //
        //此源代码改编自：
        // https://github.com/thomasmacpherson/piface/blob/master/python/piface/pfio.py
        //
        //有关SPI和WiringPi的更多详细信息，请参阅此博客文章
        // http://wiringpi.com/reference/spi-library/
        //
        //请参见下面的链接，了解MCP23S17芯片上的数据手册：
        // http://ww1.microchip.com/downloads/en/devicedoc/21952b.pdf

        System.out.println("<--Pi4J--> SPI test program using MCP23S17 I/O Expander Chip");

        // configuration
        byte IODIRA = 0x00; // I/O direction A
        byte IODIRB = 0x01; // I/O direction B
        byte IOCON  = 0x0A; // I/O config
        byte GPIOA  = 0x12; // port A
        byte GPIOB  = 0x13; // port B
        byte GPPUA  = 0x0C; // port A pullups
        byte GPPUB  = 0x0D; // port B pullups
        byte OUTPUT_PORT = GPIOA;
        byte INPUT_PORT  = GPIOB;
        byte INPUT_PULLUPS = GPPUB;

        // setup SPI for communication
        int fd = Spi.wiringPiSPISetup(0, 10000000);
        if (fd <= -1) {
            System.out.println(" ==>> SPI SETUP FAILED");
            return;
        }

        // initialize
        write(IOCON,  0x08);  // 启用硬件寻址
        write(GPIOA,  0x00);  // 设置端口A关闭
        write(IODIRA, 0);     // 将端口A设置为输出
        write(IODIRB, 0xFF);  // 将端口B设置为输入
        write(GPPUB,  0xFF);  // 设置端口B上拉

        int pins = 1;

        while(true) {
        	//将位移到A寄存器的左侧
            //这将导致下一个LED点亮
            //当前LED关闭。
            if(pins >= 255)
                pins=1;
            write(GPIOA,  (byte)pins);
            pins = pins << 1;
            Thread.sleep(1000);

            // read for input changes
            //read(INPUT_PORT);
        }
    }

    public static void write(byte register, int data){

        // 发送测试ASCII消息
        byte packet[] = new byte[3];
        packet[0] = WRITE_CMD;  // 地址字节
        packet[1] = register;  // 注册字节
        packet[2] = (byte)data;  // 数据字节

        System.out.println("-----------------------------------------------");
        System.out.println("[TX] " + bytesToHex(packet));
        Spi.wiringPiSPIDataRW(0, packet, 3);
        System.out.println("[RX] " + bytesToHex(packet));
        System.out.println("-----------------------------------------------");
    }

    public static void read(byte register){

        // send test ASCII message
        byte packet[] = new byte[3];
        packet[0] = READ_CMD;    // 地址字节
        packet[1] = register;    // 注册字节
        packet[2] = 0b00000000;  // 数据字节

        System.out.println("-----------------------------------------------");
        System.out.println("[TX] " + bytesToHex(packet));
        Spi.wiringPiSPIDataRW(0, packet, 3);
        System.out.println("[RX] " + bytesToHex(packet));
        System.out.println("-----------------------------------------------");
    }

    public static String bytesToHex(byte[] bytes) {
        final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for ( int j = 0; j < bytes.length; j++ ) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}